package com.lankystar.sos.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;


public class PushExample {
    protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

    // demo App defined in resources/jpush-api.conf 
    private static final String appKey ="316f88d8e8903d823b7d8817";
	private static final String masterSecret = "1d85081ca2c3771190b0267a";
	
	public static final String TITLE = "Test from API example";
    public static final String ALERT = "Test from API Example - alert";
    public static final String MSG_CONTENT = "Test from API Example - msgContent";
    public static final String REGISTRATION_ID = "0900e8d85ef";
    public static final String TAG = "tag_api";
    JPushClient jpushClient = new JPushClient(masterSecret, appKey);

	public static void sendPush(String [] arr, Integer sosId,Integer type) {
		 ClientConfig config = ClientConfig.getInstance();
	        config.setPushHostName("https://api.jpush.cn");
	        Map<String, String> map = new HashMap<>();
	        map.put("key", String.valueOf(sosId));
	        map.put("type", "1");
	        @SuppressWarnings("deprecation")
			JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config);
//	        PushPayload payload = PushPayload.newBuilder()
//	                .setPlatform(Platform.all())
//	                .setAudience(Audience.alias(arr))
//	                .setNotification(Notification.alert("求救,请求帮助！！"))
//	                .setOptions(Options.newBuilder()
//                          .setApnsProduction(false)
//                          .setMap(map)
//                          .build())
//	                .build();
	        PushPayload payload = PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.alias(arr))
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .setAlert("求救,请求帮助！！")
	                                .setBadge(1)
	                                .setSound("happy")
	                                .addExtras(map)
	                                .build())
	                        .build())
	                .setOptions(Options.newBuilder()
                          .setApnsProduction(false)
                          .build())
	                .build();

	        try {
	            PushResult result = jpushClient.sendPush(payload);
	            LOG.info("Got result - " + result);

	        } catch (APIConnectionException e) {
	            LOG.error("Connection error. Should retry later. ", e);

	        } catch (APIRequestException e) {
	            LOG.error("Error response from JPush server. Should review and fix it. ", e);
	            LOG.info("HTTP Status: " + e.getStatus());
	            LOG.info("Error Code: " + e.getErrorCode());
	            LOG.info("Error Message: " + e.getErrorMessage());
	            LOG.info("Msg ID: " + e.getMsgId());
	        }
	}
	
	
}

